<template>
  <div class="m-2 p-3">
    <el-button size="small" type="success" @click="addModal">新增渠道</el-button>

    <el-table v-loading="loading" :data="tableData" class="mt-2" size="small">
      <el-table-column label="渠道类型" prop="id" width="200">
        <template slot-scope="{row}">
          <span>{{ row.pay_channel | changeChannel }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付渠道" prop="id" width="200">
        <template slot-scope="{row}">
          <span>{{ row.pay_type|changeType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商户主体" prop="activity_name" width="300">
        <template slot-scope="{row}">
          <span v-if="row.business">{{ row.business.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" prop="is_open" width="300">
        <template slot-scope="{row}">
          <el-tag :type="row.status == 1 ? 'success':row.status == 0? 'info':'danger'">{{
              row.status == 1 ? '上架' : row.status == 0 ? '下架' : '异常'
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="切换逻辑" prop="activity_name" width="300">
        <template slot-scope="{row}">
          <span>{{ row.switch_logic_type|changeLginType }}</span>
        </template>
      </el-table-column>

      <el-table-column label="金额区间" prop="activity_gift_prize" width="200">
        <template slot-scope="{row}">
          <span>{{ row.min_money|formatBigNumber }}-{{ row.max_money|formatBigNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="{row}">
          <el-button size="small" type="primary" @click="toAddOrEdit(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="meta.page"
      :page-size="15"
      :total="meta.total"
      background
      class="mt-3 text-right"
      layout="total, prev, pager, next"
      @current-change="handleCurrentChange"
    >
    </el-pagination>

    <add-or-edit ref="addOrEdit" @initData="initData"/>
  </div>
</template>

<script>
import AddOrEdit from '@/views/business/components/AddOrEdit.vue'
import request from '../../utils/request'

export default {
  name: 'activeConfiguration',
  components: { AddOrEdit },
  data() {
    return {
      loading: false,
      currentPage: 1,
      meta: {
        page: 1,
        perPage: 10,
        total: undefined
      },
      tableData: [],

      /* 礼物列表 */
      giftList: []
    }
  },
  computed: {
    addOrEdit() {
      return this.$refs.addOrEdit
    }
  },
  filters: {
    formatLimitNumber(value) {
      if (value === 99999) return '无限制'
      return value
    },
    changeChannel(value) {
      switch (value) {
        case 'official':
          return '官方'
        case 'sand':
          return '杉德'
        case 'ada':
          return 'Ada'
        case 'yl':
          return '移领'
        default:
          break
      }
    },
    changeType(value) {
      switch (value) {
        case 'ali':
          return '支付宝'
        case 'wechat':
          return '微信'
        default:
          return '微信H5'
          break
      }
    },
    changeLginType(value) {
      if (value.includes(',')) {
        let str = []
        value.split(',').map((item) => {
          if (item == 'recharge') {
            str.push('充值区间')
          } else {
            str.push('白名单用户')
          }
        })
        return str.join(',')
      } else {
        if (value == 'recharge') {
          return '充值区间'
        } else {
          return '白名单用户'
        }
      }
    },
    formatBigNumber(number) {
      if (number < 10000) {
        return number
      } else {
        return `${((number) / 10000)}￥`
      }
    }

  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      this.loading = true
      const { data: { total, data } } = await request('/merchant/businessConfig/list', {
        params: this.meta,
        per_page: '15'
      })
      this.tableData = data
      this.meta.total = total
      this.loading = false
    },
    addModal() {
      this.addOrEdit.show()
    },
    toAddOrEdit(row) {
      this.addOrEdit.show(row)
    },
    handleCurrentChange(page = this.meta.page) {
      this.meta.page = page
      this.initData()
    }
  }
}
</script>
